package com.myssoteka.server;

import java.util.Random;

import javax.servlet.http.HttpSession;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.myssoteka.client.UserServices;
import com.myssoteka.shared.dao.UserDao;
import com.myssoteka.shared.domain.User;

public class UserServicesImpl extends RemoteServiceServlet implements UserServices
 {

	@Override
	public String login(String username, String password)
			throws IllegalArgumentException {
		UserDao dao = UserDao.getInstance();
		User user = dao.getUserByUsername(username);
		if(user!=null && user.getPassword().equals(password)){
			String sessionId = new Long(new Random().nextLong()).toString();
			HttpSession session = this.getThreadLocalRequest().getSession();
			session.setAttribute("sid", sessionId);
			session.setAttribute("userId", user.getId());
			return sessionId;			
		}
		return "0";
	}

}
